﻿#region

using CDNSync.Providers;
using CDNSync.Providers.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

#endregion

namespace CDNSync.Test
{
    [TestClass]
    public class SynchronizerTest
    {
        private MockRepository mocks;
        private IProviderClient providerClient;
        private IProviderClientFactory providerClientFactory;
        private ISynchronizer synchronizer;

        [TestInitialize]
        public void InitTest()
        {
            mocks = new MockRepository();
            providerClientFactory = mocks.Stub<IProviderClientFactory>();
            providerClient = mocks.Stub<IProviderClient>();

            synchronizer = new Synchronizer(providerClientFactory);
        }

        [TestMethod]
        [DeploymentItem("bin\\Test.txt")]
        public void SyncFiles_With_NewBucket_Should_Return_True()
        {
            var settings = new SyncFilesRequest
                               {
                                   BucketName = "CdnSyncTest",
                                   UserName = "username",
                                   Password = "password",
                                   Provider = CDNProvider.Amazon,
                                   Region = BucketRegion.Singapore,
                                   Files = new[] {"Test.txt"}
                               };


            var createBucketResult = new CreateBucketResult();
            var uploadFilesResult = new UploadFilesResult();

            using (mocks.Record())
            {
                SetupResult
                    .For(providerClientFactory.Create(CDNProvider.Amazon))
                    .Return(providerClient);

                SetupResult
                    .For(providerClient.Authenticate(settings.UserName, settings.Password))
                    .Return(true);

                SetupResult
                    .For(providerClient.BucketExists(settings.BucketName))
                    .Return(false);

                SetupResult
                    .For(providerClient.CreateBucket(settings.BucketName, settings.Region))
                    .Return(createBucketResult);


                SetupResult
                    .For(providerClient.Upload(null, settings.BucketName)).IgnoreArguments()
                    .Return(uploadFilesResult);
            }


            var result = synchronizer.SyncFiles(settings);

            Assert.IsTrue(result.WasSuccessful);
        }


        [TestMethod]
        [DeploymentItem("bin\\Test.txt")]
        public void SyncFiles_With_ExistingBucket_Should_Return_True()
        {
            var settings = new SyncFilesRequest
                               {
                                   BucketName = "CdnSyncTest",
                                   UserName = "username",
                                   Password = "password",
                                   Provider = CDNProvider.Amazon,
                                   Region = BucketRegion.Singapore,
                                   Files = new[] {"Test.txt"}
                               };

            var uploadFilesResult = new UploadFilesResult();

            using (mocks.Record())
            {
                SetupResult
                    .For(providerClientFactory.Create(CDNProvider.Amazon))
                    .Return(providerClient);

                SetupResult
                    .For(providerClient.Authenticate(settings.UserName, settings.Password))
                    .Return(true);

                SetupResult
                    .For(providerClient.BucketExists(settings.BucketName))
                    .Return(true);

                SetupResult
                    .For(providerClient.Upload(null, settings.BucketName)).IgnoreArguments()
                    .Return(uploadFilesResult);
            }


            var result = synchronizer.SyncFiles(settings);

            Assert.IsTrue(uploadFilesResult.WasSuccessful);
        }
    }
}